<?php

namespace App\Utils;

class Assert
{
    /**
     * @param string $message
     * @param $e
     * @throws \Exception
     */
    private static function execute(string $message, $e = null)
    {
        if (is_null($e)) {
            throw new \Exception($message);
        } else {
            if ($e instanceof \Exception) {
                throw $e;
            } else {
                throw new $e();
            }
        }
    }

    /**
     * @param bool $true
     * @param $e
     * @param string $message
     * @throws \Exception
     */
    public static function isTrue(bool $true, $e = null, string $message = '')
    {
        if (true !== $true) {
            self::execute($message ?: 'not true', $e);
        }
    }

    /**
     * @param $obj
     * @param \Exception|null $e
     * @param string $message
     * @throws \Exception
     */
    public static function notNull($obj, $e = null, string $message = '')
    {
        if (is_null($obj)) {
            self::execute($message ?: '值不为空', $e);
        }
    }

    /**
     * @param int $a
     * @param int $b
     * @param \Exception|string $e
     * @throws \Exception
     */
    public static function equals(int $a, int $b, $e = null)
    {
        if ($a !== $b) {
            self::execute('值不相等', $e);
        }
    }

    /**
     * @param int $a
     * @param int $b
     * @param \Exception|null $e
     * @throws \Exception
     */
    public static function notEquals(int $a, int $b, \Exception $e = null)
    {
        if ($a === $b) {
            self::execute('值相等', $e);
        }
    }
}
